package com.lwp.springboot.sbmybatis.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * app.datasource.first.url=jdbc:mysql://localhost/first
 * app.datasource.first.username=dbuser
 * app.datasource.first.password=dbpass
 * app.datasource.first.configuration.maximum-pool-size=30
 *
 * app.datasource.second.url=jdbc:mysql://localhost/second
 * app.datasource.second.username=dbuser
 * app.datasource.second.password=dbpass
 * app.datasource.second.max-total=30
 */
//@Configuration(proxyBeanMethods = false)
public class AppDataSourcesConfig {
    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.first")
    public DataSourceProperties firstDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.first.configuration")
    public HikariDataSource firstDataSource(DataSourceProperties firstDataSourceProperties) {
        return firstDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    /**
     * 使用common
     * @return
     */
    @Bean
    @ConfigurationProperties("app.datasource.second")
    public BasicDataSource secondDataSource() {
        return DataSourceBuilder.create().type(BasicDataSource.class).build();
    }

//    @Bean(initMethod = "init", destroyMethod = "close")
    @Bean
//    @ConfigurationProperties("app.datasource.first.configuration")
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource druidDataSource(DataSourceProperties firstDataSourceProperties) {
        return firstDataSourceProperties.initializeDataSourceBuilder().type(DruidDataSource.class).build();
    }



    /**
     * 少的配置
     * @return
     */
/*    public DataSource druidDataSource1() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        //configuration
        dataSource.setInitialSize(5); // 默认 0
        dataSource.setMinIdle(5); // 默认0
        dataSource.setMaxActive(20); // 默认8
        dataSource.setPoolPreparedStatements(true); // 默认false
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20); // 默认 10

        dataSource.setValidationQuery("SELECT 1 FROM DUAL");
        dataSource.setMaxWait(60_000); // 默认 -1 一直等待
        try {
//            String filters = "stat,wall,log4j"; // 配置监控统计拦截的filters
            String filters = "stat";
            dataSource.setFilters(filters);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 如果配置了proxyFilters，此配置可以不配置
        // druid.stat.mergeSql=true 合并执行的相同sql，避免因为参数不同而统计多条sql语句
        // druid.stat.slowSqlMillis=10000 用来配置SQL慢的标准，执行时间超过slowSqlMillis的就是慢
        // dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ");
        return dataSource;
    }*/

    /**
     * @return
     */
/*    public DataSource druidDataSource1() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        //configuration
        dataSource.setInitialSize(5); // 默认 0
        dataSource.setMinIdle(5); // 默认0
        dataSource.setMaxActive(20); // 默认8
        dataSource.setPoolPreparedStatements(true); // 默认false
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20); // 默认 10

        *//*用来检测连接是否有效的sql，要求是一个查询语句。
        如果validationQuery为null，testOnBorrow、testOnReturn、
        testWhileIdle都不会其作用*//*
        dataSource.setValidationQuery("SELECT 1 FROM DUAL");
//        dataSource.setTestWhileIdle(true); // 默认 true
//        dataSource.setOnBorrow(false); // 默认 false
//        dataSource.setOnReturn(false); // 默认 false
        dataSource.setMaxWait(60_000); // 默认 -1 一直等待
        // 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
//        dataSource.setTimeBetweenEvictionRunsMillis(60000); // 默认60s
        // minEvictableIdleTimeMillis 配置一个连接在池中最小生存的时间，单位是毫秒
//        dataSource.setMinEvictableIdleTimeMillis(180_0000); // 30分钟

        try {
//            String filters = "stat,wall,log4j"; // 配置监控统计拦截的filters
            String filters = "stat";
            dataSource.setFilters(filters);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 如果配置了proxyFilters，此配置可以不配置
        // druid.stat.mergeSql=true 合并执行的相同sql，避免因为参数不同而统计多条sql语句
        // druid.stat.slowSqlMillis=10000 用来配置SQL慢的标准，执行时间超过slowSqlMillis的就是慢
        // dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ");
        return dataSource;
    }*/
}
